package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Timer;

public class Relogio extends JFrame {
	
	private static final long serialVersionUID = -1039785471529622454L;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	private JTextField txt;
	
	public Relogio() {
		setTitle("Clock");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		txt = new JTextField(12);
		
		txt.setText( sdf.format( new Date() ) );
		this.getContentPane().add(  txt );
		this.pack();
		this.setMinimumSize( this.getPreferredSize() );
		setLocationRelativeTo( null );

		//chamo o uso dele na minha janela
		update();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Relogio().setVisible(true);
	}

	/**
	 * Evento de atualizar qualquer coisa 
	 */
	public void update() {
		
		/**
		 * o timer funciona em millisegundo
		 */
		Timer time = new Timer(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				txt.setText( sdf.format( new Date() ) );
			}

		});

		//apos cria-lo tem que start 
		time.start();
		
	}
	
}
